package com.captjack.common.util.poi;

import com.captjack.common.util.io.IoCommonUtil;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ----------------------------------------------------------------------------- <br>
 * project name ：micro-service <br>
 * function：CsvUtils。。。。。 <br>
 * description：CsvUtils。。。。。 <br>
 * ----------------------------------------------------------------------------- <br>
 * change history <br>
 * <table width="432" border="1">
 * <tr>
 * <td>version</td>
 * <td>time</td>
 * <td>author</td>
 * <td>change</td>
 * </tr>
 * <tr>
 * <td>1.0.0</td>
 * <td>2018/11/16 23:43</td>
 * <td>Jack Sparrow</td>
 * <td>create</td>
 * </tr>
 * </table>
 * <br>
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/11/16 23:43
 * package com.captjack.common.util.poi
 */
public final class CsvUtils {

    /**
     * 将Map集合生成csv文件，表头需要手动初始化
     *
     * @param exportList 需要导出的集合
     * @param header     导出表头
     * @param exportPath 导出文件的路径
     */
    public static void exportCsvByMap(Iterable<Map<String, Object>> exportList, String[] header, String exportPath) throws Exception {
        OutputStreamWriter outputStreamWriter = null;
        try {
            CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(header);
            // 字符输出流
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File(exportPath)));
            CSVPrinter csvPrinter = new CSVPrinter(outputStreamWriter, csvFormat);
            // 写数据
            for (Map<String, Object> map : exportList) {
                csvPrinter.printRecord(map.values());
            }
            outputStreamWriter.flush();
        } finally {
            IoCommonUtil.closeStream(outputStreamWriter);
        }
    }

    /**
     * 读取csv文件
     *
     * @param filePath    文件路径
     * @param totalColumn 总列数
     * @return list
     * @throws Exception 异常
     */
    public static List<Map<String, String>> readData(String filePath, int totalColumn) throws Exception {
        String[] header = new String[totalColumn];
        for (int i = 0; i < totalColumn; i++) {
            header[i] = String.valueOf(i);
        }
        return readData(CSVFormat.DEFAULT, header, filePath);
    }

    /**
     * 读取csv文件
     *
     * @param csvFormat format type
     * @param headers   头信息
     * @param filePath  文件路径
     * @return 读取结果
     * @throws Exception 异常
     */
    public static List<Map<String, String>> readData(CSVFormat csvFormat, String[] headers, String filePath) throws Exception {
        List<Map<String, String>> result = new ArrayList<>();
        // 字符流reader
        Reader reader = null;
        try {
            reader = new FileReader(filePath);
            CSVParser csvRecords = csvFormat.withHeader(headers).parse(reader);
            // 遍历读取
            for (CSVRecord csvRecord : csvRecords) {
                result.add(csvRecord.toMap());
            }
            return result;
        } finally {
            IoCommonUtil.closeStream(reader);
        }
    }

}
